jDcp.web.Form = function(){
	
	this.form = null;
	this.relativeTable = null;
	var _this = null;
	this.url = null;
	
	this.parse = function(htmlData){
		this.form = $(htmlData);
		_this = this;
		
		//TODO: 添加事件
		if(this.form != null){
			this.form.find("a.save").click(function(){
				var elements = _this.form.find(".jform-element");
				var table = _this.form.attr("relativeTable");
				var sql = "INSERT INTO " + table + "(#COLLIST#) VALUES (#VALUES#)";
				if(_this.form.attr("sqltype")){
					var colList = "";
					var values = "";
					$.each(elements,function(i,n){
						var name = $(this).attr("name");
						var value = $(this).val()||$(this).text();
						var colType = $(this).attr("coltype");
						if(i > 0) { 
							colList += ",";
							values += ",";
						}
						colList += name;
						
						if("NUMBER" == colType){
							values += value;
						} else {
							values += "'" + value + "'";
						}
					});
					sql = sql.replace(/#COLLIST#/g, colList).replace(/#VALUES#/g,values);
					console.log("jForm run SQL:{"+sql+"}");
				}
				var request = {};
				request.action = "runsql";
				request.sqls = sql;
				request.rand = Util.getRandom();
				$.post("/DCP3/jTable",request,function(data){
					var obj = {};
					obj.context = "数据保存成功！";
					obj.url = _this.getUrl();
					Util.openDialog(obj);
				});
			});
		}
	};
	
	this.getItemValue = function(name){
		var item = _this.form.find("*[name='"+name+"']");
		console.log("name = "+name+"\tvalue = "+ item.text()||item.val());
		return item.text()||item.val();
	};
	
	this.addButton = function(option){
		var container = _this.form.find(".jform-footer");
		var button = $("<button></button>");
		var text = option.text || "未命名按钮";
		var id = option.id || Util.getRandom();
		var cls = option.cls || "btn";
		var href = option.href || "#";
		button.text(text).addClass(cls).addClass("ml10").attr("id",id).attr("disabled",option.disabled);
		if(href != "#"){
			button.click(function(){
				window.location.replace(href);
			});
		}	
		container.append(button);
	};
	
	this.setUrl = function(url){
		this.url = url;
	};
	
	this.getUrl = function(){
		return this.url;
	};
	
	this.appendTo = function(id){
		var container = "#" + id;
		$(container).append(this.form);
	};
	
};